#include<stdio.h>
#include<conio.h>
#include<math.h>
int s,m,M,n,e,d,C;
int e_gcd()
{
	 d=1;
     do {	s=(d*e)%m;
          	d++;
     	} while(s!=1); 
     d-=1;
}
int E() //This function computes C=M^e mod n
{    C=1;
     for(int i=0;i<e;i++)
     {  C=C*M%n;
     	C=C%n;	
     }
     printf("\nEncrypted Keyword: %d",C);     
}
int D() //This function computes M=C^d mod n
{    M=1;
     for(int i=0;i<d;i++)
     {  M=M*C%n;
     	M=M%n;	
     }
     printf("\nDecrypted Keyword: %d",M);     
}
int main()
{    int p,q;
     printf("\nInput 2 prime numbers: "); scanf("%d%d",&p,&q);
	 n=p*q; m=(p-1)*(q-1);
     printf("\nm=%d",m);
	 printf("\nEnter e:"); scanf("%d",&e); 
	 e_gcd(); //Execute the e_gcd function
     printf("\nPublic key: <%d,%d> -- Secret key: <%d,%d>",e,n,d,n);
     printf("\nInput the plain text M: "); scanf("%d",&M);
     D(); //Execute the encrypt function
     printf("\nInput the Cipher text C: "); scanf("%d",&C);
     E(); //Execute the decrypt function
     getch();
}
